package com.lagou.rabbitmq.controller;

import com.lagou.rabbitmq.bean.Order;
import com.lagou.rabbitmq.service.OrderService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/createOrder")
    public ModelAndView createOrder() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("pay");
        Order order = orderService.createOrder();
        mv.addObject("orderId", order.getId());
        mv.addObject("expiredTime",
                DateFormatUtils.format(order.getExpireTimestamp(), "yyyy-MM-dd HH:mm:ss"));
        return mv;
    }

    @RequestMapping("/pay")
    public ModelAndView pay(@RequestParam Integer orderId) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("payResult");
        mv.addObject("result", orderService.pay(orderId));
        return mv;
    }

    @RequestMapping("/orders")
    public ModelAndView orders() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("orders");
        mv.addObject("orders", orderService.getOrders());
        return mv;
    }
}
